const TimerUtils = function () {
    return this;
};
TimerUtils.prototype = {
    constructor: TimerUtils,

    _time: 0,

    /**
     * 是否可用.
     * @returns {boolean}
     */
    isEnable: function () {
        return this._time < 1;
    },

    /**
     * 开始倒计时.
     * @param containerId 容器ID.
     * @param time 计时时间
     * @param textDisable 计时文字，如：[time]后可重发
     * @param textEnable 恢复后的文字，默认取传入容器ID原有html内容.
     */
    start: function (containerId, time, textDisable, textEnable) {
        const obj = $('#' + containerId);
        textEnable = textEnable || obj.html();
        const self = this;
        this._time = time;
        if (time < 1) {
            obj.attr('disabled', '');
            obj.html(textEnable);
            return;
        }
        obj.attr('disabled', true);
        obj.attr('disabled', 'disabled');
        obj.html(textDisable.replace("[time]", time));
        setTimeout(function () {
            self.start(containerId, time-1, textDisable, textEnable);
        }, 1000);
    }

};

window.QiTimerUtils = new TimerUtils();